package net.trustie.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.codec.digest.DigestUtils;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

public class slashdot_Model implements AfterExtractor, ValidateExtractor {
	private String post_id="";
	private String post_url;
	@ExtractBy("//h2[@class='story']/span/a/text()")
	private String post_title;
	@ExtractBy("//div[@class='body']/div/allText()")
	private String post_content;
	@ExtractBy("//header/div/a/text()")
	private String author;
	@ExtractBy("//header/div/a/@href")
	private String author_url;
	@ExtractBy("//span[@class='totalcommentcnt']/text()")
	private String reply_num;
	private String extractTime;
	@ExtractBy("//header/div/time/text()")
	private String post_time;
	private int history = 0;

	private String source = "slashdot";
	private String type="news";
	private String url_md5;
	private String view_num;
	
	private String slashdot_source;
	
	private List<String> related_posts;
	private String related_post;
	
	private List<String> tags;
	private String tag;
	public void afterProcess(Page page) {
		// 对帖子URL进行处理
		this.post_url = page.getPageUrl();
		this.url_md5 =  DigestUtils.md5Hex(this.post_url);
		// 对帖子ID进行处理
		String s1=post_url.substring(0,post_url.lastIndexOf("/"));
		this.post_id=s1.substring(s1.lastIndexOf("/")+1);
		// 得到抽取时间
		this.extractTime = DateHandler.getExtractTime();

		// 对帖子的发布时间进行处理
		/* String post_time = "on Tuesday February 21, 2015 @07:25PM "; */
		String str = post_time.substring(3).replace("@", "");
		String str1 = str.substring(str.indexOf(" ") + 1);
		String pat = "MMM dd, yyyy hh:mma";
		SimpleDateFormat sdf = new SimpleDateFormat(pat, Locale.US);
		Date date = null;
		try {
			date = sdf.parse(str1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String pat2 = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf1 = new SimpleDateFormat(pat2);
		this.post_time = sdf1.format(date);
	}

	public void validate(Page page) {
		if (StringHandler.isAtLeastOneBlank(this.post_title, this.post_url,
				this.extractTime)) {
			page.setResultSkip(this, true);
			return;
		}

		if (!StringHandler.canFormatterInteger(this.reply_num)) {
			page.setResultSkip(this, true);
			return;
		}

		if (!DateHandler.canFormatToDate(this.post_time, this.extractTime)) {
			page.setResultSkip(this, true);
			return;
		}
	}

	public String getPost_id() {
		return post_id;
	}

	public void setPost_id(String post_id) {
		this.post_id = post_id;
	}

	public String getPost_url() {
		return post_url;
	}

	public void setPost_url(String post_url) {
		this.post_url = post_url;
	}

	public String getPost_title() {
		return post_title;
	}

	public void setPost_title(String post_title) {
		this.post_title = post_title;
	}

	public String getPost_content() {
		return post_content;
	}

	public void setPost_content(String post_content) {
		this.post_content = post_content;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getAuthor_url() {
		return author_url;
	}

	public void setAuthor_url(String author_url) {
		this.author_url = author_url;
	}

	public String getReply_num() {
		return reply_num;
	}

	public void setReply_num(String reply_num) {
		this.reply_num = reply_num;
	}

	public String getExtractTime() {
		return extractTime;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public String getPost_time() {
		return post_time;
	}

	public void setPost_time(String post_time) {
		this.post_time = post_time;
	}

	public int getHistory() {
		return history;
	}

	public void setHistory(int history) {
		this.history = history;
	}

}
